package VendingMachine;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import junit.framework.*;

public class VendingMachineTest extends TestCase {
    /* Before test the function of Vending Machine, I create some VendingMachineitems with some data to wait for test
     *  and declare four slots, next step, I will test whether the functions of Vending Machine can run based these 
     *  data     */
	 VendingMachineItem  product1, product2;
	 VendingMachine VM= null;
	 String aa = VendingMachine.A_CODE;
     String bb = VendingMachine.B_CODE;
     String cc= VendingMachine.C_CODE;
     String dd = VendingMachine.D_CODE; 

     @Before
	 public void setUp() throws Exception {
       
	   product1 = new VendingMachineItem( "chocolate",2.99);
	   product2 = new VendingMachineItem( "biscuit",1.99);
	   VM= new VendingMachine();
	   }
	  
	   @After
	   public void tearDown() throws Exception 
	   {    } 
  
     /* test additem function. I write two test case.
      * I try to add product1 and product2 in slot(A_CODE and B_CODE)
      * */
       @Test 
	   public void testaddItem() throws VendingMachineException
	   {
		VM.addItem(product1,aa);
		assertEquals(product1,VM.getItem(aa)); 

		VM.addItem(product2,bb);
		assertEquals(product2,VM.getItem(bb)); 
	   }
       /* test removeItem function. I write two test case.
        * I try to add product1 and product2 in slot(A_CODE and B_CODE)
        * */
       public void testremoveItem(){
         VM.addItem(product1, aa);
         assertEquals(product1,VM.removeItem(aa));  
         
         VM.addItem(product2,bb);
         assertEquals(product2,VM.removeItem(bb));  
       }
       /* I will insert money to vending machine and utilize */
       public void testinsertMoney()
       {  VM.insertMoney(1.00);
       assertEquals(1,VM.getBalance(), 0.001);
       }
       /* I test add money several times and get total Balance*/
       public void testgetBalance(){
           
    	   VM.insertMoney( 0.25 );
          assertEquals( 0.25, VM.getBalance(), 0.001 );
           VM.insertMoney( 0.1 );
           assertEquals( 0.35, VM.getBalance(), 0.001 );
           VM.insertMoney(1.00);
           assertEquals(1.35,VM.getBalance(),0.001);
           VM.insertMoney( 0.0 );
           assertEquals(1.35,VM.getBalance(),0.001);
       }
       
       
       /*I make two status to test makePurchase() function.
        * first the vending machine has enough money
        * next, I do not insert the vending machine enough money and see if the machine can stop transaction.
        * */
       public void testmakePurchase(){
    	   boolean result;
    	   boolean result1;
    	    VM.addItem(product1, aa);
    	    VM.insertMoney(3.00);
    	     result = VM.makePurchase( aa );
            assertEquals( true, result );     
            
            VM.addItem(product2, bb);
            VM.insertMoney(0);
              result1 =VM.makePurchase(bb);
              assertEquals( false, result1 ); 
       }
       /*the function test returnchange after finishing transaction. however, there is a bug in the source code.
        * "this.balance = 0;" the balance will return the zero, no change will return. I have to delete the code and make test
        * run.
        * */
       public void testReturnChange() {
    	   
         Double change = VM.returnChange();  
         boolean result;
         VM.insertMoney( 0.00);
         assertEquals( 0.0, change, 0.001 );
         
         VM.insertMoney( 6.00 );
         change = VM.returnChange();
         assertEquals( 6.00, change, 0.001 );
         
         
         VM.addItem( product1, aa);
         result = VM.makePurchase(aa);
         assertEquals( true, result );  
         change = VM.returnChange();
         assertEquals( 3.01, change, 0.001 );
         
         
         VM.addItem( product2, bb);
         result = VM.makePurchase(bb);
         assertEquals( true, result );  
         change = VM.returnChange();
         assertEquals( 1.02, change, 0.001 );
      
         VM.addItem( product2, bb);
         result = VM.makePurchase(bb);
         assertEquals( false, result );  
         change = VM.returnChange();
         assertEquals( 1.02, change, 0.001 );
   }
}
         

    

